package edu.sias.dao;

import edu.sias.pojo.S_User;
import edu.sias.util.DButil;

import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDao {

    //完成登录的查询
    public boolean queryUser(String name,String psw){
        String sql = "select * from s_user " +
                "where username=? and password=?";
        //查询操作
        ResultSet rs = DButil.select(sql, new Object[]{name, psw});
        //判断结果集中是否存在数据
        try {
            if (rs.next()){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    //注册用户信息操作
    public void insertUser(S_User s_user){
        System.out.println("s_user = " + s_user);
        String sql = "insert into s_user "+
                "values(null,?,?,?,?,?,?)";
        Object[] obs = {s_user.getUsername(),
                s_user.getPassword(),
                s_user.getEmail(),
                s_user.getPhone(),
                s_user.getGender(),
                s_user.getAddress()};
        //执行添加保存用户信息操作
        DButil.update(sql,obs);


    }

    //根据用户名获取用户信息
    public S_User queryUserByName(String name) throws SQLException {

        String sql =
                "select * from s_user where name =?";
        S_User user = null;
        ResultSet rs =
                DButil.select(sql, new Object[]{name});
        while (rs.next()){
            int id = rs.getInt("id");
            String uname = rs.getString("name");
            String email = rs.getString("email");
            String phone = rs.getString("phone");
            String gender = rs.getString("gender");
            String address = rs.getString("address");
            user = new S_User(id,uname,email,phone,gender,address);
        }
        return user;
    }
}
